package org.dru.clay.respository.artifact;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ArtifactParserImpl implements ArtifactParser {
	final Pattern overall = Pattern.compile("([a-z0-9_]+)-([0-9\\.]+)(-[^\\.]+)?(\\.[a-z]+)?");

	@Override
	public UnresolvedArtifact parseResolved(String artifactString) throws InvalidNameException {
		final Matcher matcher = overall.matcher(artifactString);
		if (!matcher.matches()) {
			throw new InvalidNameException("Invalid artifact name: " + artifactString);
		}
		final String name = matcher.group(1);
		final VersionPattern version = new VersionPattern(matcher.group(2));
		return new UnresolvedArtifact(name, version);
	}
}
